package routes

import (
	"gin-blog/controllers"
	"gin-blog/middleware"

	"github.com/gin-gonic/gin"
)

func SetupRoutes(r *gin.Engine) {
	// 初始化控制器
	authController := &controllers.AuthController{}
	postController := &controllers.PostController{}
	commentController := &controllers.CommentController{}
	categoryController := &controllers.CategoryController{}

	// API路由组
	api := r.Group("/api/v1")

	// 健康检查
	api.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{"status": "ok", "message": "Blog API is running"})
	})

	// 认证路由
	auth := api.Group("/auth")
	{
		auth.POST("/register", authController.Register)
		auth.POST("/login", authController.Login)
		auth.GET("/profile", middleware.AuthMiddleware(), authController.GetProfile)
		auth.PUT("/profile", middleware.AuthMiddleware(), authController.UpdateProfile)
	}

	// 分类路由
	categories := api.Group("/categories")
	{
		categories.GET("", categoryController.GetCategories)
		categories.GET("/:id", categoryController.GetCategory)
		categories.POST("", middleware.AuthMiddleware(), middleware.AdminMiddleware(), categoryController.CreateCategory)
		categories.PUT("/:id", middleware.AuthMiddleware(), middleware.AdminMiddleware(), categoryController.UpdateCategory)
		categories.DELETE("/:id", middleware.AuthMiddleware(), middleware.AdminMiddleware(), categoryController.DeleteCategory)
	}

	// 文章路由
	posts := api.Group("/posts")
	{
		posts.GET("", middleware.OptionalAuthMiddleware(), postController.GetPosts)
		posts.GET("/:id", middleware.OptionalAuthMiddleware(), postController.GetPost)
		posts.POST("", middleware.AuthMiddleware(), postController.CreatePost)
		posts.PUT("/:id", middleware.AuthMiddleware(), postController.UpdatePost)
		posts.DELETE("/:id", middleware.AuthMiddleware(), postController.DeletePost)
		posts.POST("/:id/like", middleware.AuthMiddleware(), postController.LikePost)
	}

	// 评论路由
	comments := api.Group("/comments")
	{
		comments.GET("/post/:post_id", commentController.GetComments)
		comments.POST("", middleware.AuthMiddleware(), commentController.CreateComment)
		comments.PUT("/:id", middleware.AuthMiddleware(), commentController.UpdateComment)
		comments.DELETE("/:id", middleware.AuthMiddleware(), commentController.DeleteComment)
		comments.PUT("/:id/moderate", middleware.AuthMiddleware(), middleware.AdminMiddleware(), commentController.ModerateComment)
	}

	// 管理员路由
	admin := api.Group("/admin")
	admin.Use(middleware.AuthMiddleware(), middleware.AdminMiddleware())
	{
		// 用户管理
		admin.GET("/users", func(c *gin.Context) {
			// TODO: 实现用户管理功能
			c.JSON(200, gin.H{"message": "Admin users endpoint"})
		})

		// 统计信息
		admin.GET("/stats", func(c *gin.Context) {
			// TODO: 实现统计信息功能
			c.JSON(200, gin.H{"message": "Admin stats endpoint"})
		})
	}
}
